package com.sundi.ecgoods.web;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.sundi.ecgoods.vo.PageVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sundi.ecgoods.entity.EcGoods;
import com.sundi.ecgoods.service.IEcGoodsService;
import com.sundi.ecgoods.utils.UploadUtil;
import com.sundi.ecgoods.vo.ResultEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 商品基本信息表 前端控制器
 * </p>
 *
 * @author jgn
 * @since 2020-02-25
 */
@RestController
@RequestMapping("/goods")
@Slf4j
public class EcGoodsController {
    @Autowired
    UploadUtil uploadUtils;

    @Autowired
    IEcGoodsService goodsService;

    @RequestMapping("list")
    public ResultEntity list(PageVo pageVo){
        PageHelper.startPage(pageVo.getPageNo(),pageVo.getPageSize());
        Wrapper<EcGoods> wrapper = new EntityWrapper<>();
        wrapper.eq("deleted",0);
        if(pageVo.getId()!=null){
            wrapper.eq("id",pageVo.getId());
        }
        if(pageVo.getName()!=null && pageVo.getName()!=""){
            wrapper.like("name",pageVo.getName());
        }
        if(pageVo.getGoodsSn()!=null && pageVo.getGoodsSn()!=""){
            wrapper.eq("goods_sn",pageVo.getGoodsSn());
        }
        if(pageVo.getCategoryId()!=null){
            wrapper.eq("category_id",pageVo.getCategoryId());
        }
        wrapper.orderBy("id",false);
        List<EcGoods> goods = goodsService.selectList(wrapper);
        PageInfo<EcGoods> pageInfo=new PageInfo<>(goods);
        return ResultEntity.ok(pageInfo);
    }

    @RequestMapping("hot")
    public ResultEntity hot(PageVo pageVo){
        PageHelper.startPage(pageVo.getPageNo(),pageVo.getPageSize());
        Wrapper<EcGoods> wrapper = new EntityWrapper<>();
        wrapper.orderBy("retail_price",true);
        List<EcGoods> goods = goodsService.selectList(wrapper);
        PageInfo<EcGoods> pageInfo=new PageInfo<>(goods);
        return ResultEntity.ok(pageInfo);
    }

    @RequestMapping("delete")
    public ResultEntity delete(Integer id){
        Wrapper<EcGoods> wrapper = new EntityWrapper<>();
        wrapper.eq("id",id);
        EcGoods goods = goodsService.selectOne(wrapper);
        goods.setDeleted(1);
        goodsService.updateById(goods);
        return ResultEntity.ok("逻辑删除成功");
    }


    @RequestMapping(value = "add")
    public ResultEntity addGoods(@ModelAttribute PageVo pageVo) throws IOException {
        List<String> upload = uploadUtils.upload(pageVo.getFiles());
        if (!upload.isEmpty()){
            pageVo.setPicUrl(upload.get(0));
        }
        pageVo.setFiles(null);
        EcGoods goods=new EcGoods();
        BeanUtils.copyProperties(pageVo,goods);
        goodsService.insert(goods);
        return  ResultEntity.ok();
    }

    @RequestMapping(value = "upd")
    public ResultEntity updGoods(@ModelAttribute PageVo pageVo) throws IOException {
        log.info("要修改的数据为:{}",pageVo);
        List<String> upload = uploadUtils.upload(pageVo.getFiles());
        if (pageVo.getImages()!=null){
            pageVo.setPicUrl(pageVo.getImages()[0]);
        }
        if (!upload.isEmpty()){
            pageVo.setPicUrl(upload.get(0));
        }
        pageVo.setFiles(null);
        EcGoods goods=new EcGoods();
        BeanUtils.copyProperties(pageVo,goods);
        goodsService.updateById(goods);
        return  ResultEntity.ok();
    }

}